package com.flowerSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.flowerSystem.config.MybatisRealm;
import com.flowerSystem.entity.User;
import com.flowerSystem.mapper.UserMapper;
import com.flowerSystem.util.ResponseResult;
import com.flowerSystem.vo.UserLoginVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RequestMapping
@RestController
@CrossOrigin
public class LoginController {
    @Autowired
    UserMapper userMapper;
    //管理端登录
    @RequestMapping("/manger/login")
    public ResponseResult login(@RequestBody User user){
        String username= user.getUsername();
        String password = user.getPassword();

        Subject subject = SecurityUtils.getSubject();
        try {
            //将用户填写的账号和密码封装到token令牌中
            UsernamePasswordToken token = new UsernamePasswordToken(username,password);

            //执行认证(调用realm中认证的方法)
            subject.login(token);
            //获取用户信息
            UserLoginVO UserLoginVO = (UserLoginVO) subject.getPrincipal();

            return ResponseResult.ok(UserLoginVO);

        }catch (AuthenticationException e){
            return ResponseResult.fail();
        }
    }
    //用户端登录
    @RequestMapping("/user/login")
    public ResponseResult userLogin(@RequestBody User user){
        String username= user.getUsername();
        String password = user.getPassword();
        Subject subject = SecurityUtils.getSubject();
        try {
            //将用户填写的账号和密码封装到token令牌中
            UsernamePasswordToken token = new UsernamePasswordToken(username,password);

            //执行认证(调用realm中认证的方法)
            subject.login(token);
            //获取用户信息
            UserLoginVO userLoginVO = (UserLoginVO) subject.getPrincipal();

            return ResponseResult.ok(userLoginVO);

        }catch (AuthenticationException e){
            return ResponseResult.fail();
        }
    }

    @PostMapping("user/reg")
    public ResponseResult creatUser(@RequestBody User user) {
        if (user != null) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("username", user.getUsername());
            User user1 = userMapper.selectOne(queryWrapper);
            if (user1 != null) {
                return ResponseResult.error("输入的用户名太火了，重新输入信息进行注册，本次注册失败！");
            }
//            user.setId(ValidateCodeUtils.generateValidateCode(8));
//            boolean save = userService.save(user);
//            int userAdd = userMapper.insert(user);
            int userId = userMapper.userRegist(user);
            if(userId > 0){
                int rows = userMapper.userRoleAdd(userId);
                if(rows > 0){
                    return ResponseResult.success("注册成功...");
                }
            }
            return ResponseResult.error("用户注册失败");
        }
        return ResponseResult.error("请按照要求输入全部信息进行注册，本次添加记录失败！");
    }

}
